package create;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: fzykd
 * Date: 2023-01-11
 * Time: 12:10
 */
//定义普通类 实现callable接口 返回的Integer类型的结果

class MyCallable implements Callable<String>{
    //重写call方法
     private int ticket=5;
    @Override
    public String call() throws Exception{
            for(int x=0;x<100;x++){
                if(this.ticket>0){
                    System.out.println("卖票，ticket="+this.ticket--);
                }
            }
            return "票已卖光!";
    }
}
//使用Callable接口 (卡漏伯 可回收的) 结局了
public class Demo6 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //创建Callable普通类
        MyCallable callable = new MyCallable();
        //使用FutureTask 用于获取线程执行的结果
        FutureTask<String> futureTask = new FutureTask<>(callable);
        //创建线程
        Thread t = new Thread(futureTask);
        //启动线程
        t.start();
        //得到线程的执行结果
        String result = futureTask.get();
        System.out.println(result);
    }
}

//FutureTask + Callable 的方式获取线程的执行结果
